<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use app\admin\controller\Base;
use app\admin\service\Purchase as PurchaseService;
use app\admin\service\PurchaseLogistics as PurchaseLogisticsService;
use app\admin\validate\PurchaseLogistics as PurchaseLogisticsValidate;

/**
 * PurchaseLogistics Controller
 *
 * 该类处理与采购物流相关的请求和服务。
 *
 * @var Request $request  请求对象，包含操作员信息。
 * @var PurchaseLogisticsService $service  采购物流服务实例。
 * @var PurchaseLogisticsValidate $validate  采购物流验证实例。
 * @var PurchaseService $purchaseService  采购服务实例。
 * @var int|null $operator_id  当前操作员的ID，如果未设置则默认为null。
 *
 * @param Request $request  请求对象。
 * @param PurchaseLogisticsService $purchaseLogisticsService  采购物流服务实例。
 * @param PurchaseLogisticsValidate $purchaseLogisticsValidate  采购物流验证实例。
 * @param PurchaseService $purchaseService  采购服务实例。
 */
class PurchaseLogistics extends Base
{
    protected $request;
    protected $service;
    protected $purchaseService;
    protected $validate;
    protected $operator_id;

    public function __construct(Request $request, PurchaseLogisticsService $purchaseLogisticsService, PurchaseLogisticsValidate $purchaseLogisticsValidate, PurchaseService $purchaseService)
    {
        $this->request = $request;
        $this->service = $purchaseLogisticsService;
        $this->validate = $purchaseLogisticsValidate;
        $this->purchaseService = $purchaseService;
        $this->operator_id = $request->operator_id ?? null; // Default to null if not set
    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $purchase_order_id  = intval($params['purchase_order_id']);
        $from  = isset($params['from']) ? $params['from'] : 'detial';
        $purchaseRes = $this->purchaseService->getInfo(['id' => $purchase_order_id]);
        if (0 !== $purchaseRes['code']) {
            return $this->returnJsonData(100400, [], $purchaseRes['message']);
        }

        $res = $this->service->getList(['purchase_order_id' => $purchase_order_id], 'id,purchase_order_id,amount,name,remark,order_number',1,100,$from);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('save')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $purchase_order_id  = intval($params['purchase_order_id']);
        $purchaseRes = $this->purchaseService->getInfo(['id' => $purchase_order_id]);
        if (0 !== $purchaseRes['code']) {
            return $this->returnJsonData(100400, [], $purchaseRes['message']);
        }

        $res = $this->service->save($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {
        if ($id <= 0) {
            return $this->returnJsonData(100001, [], '参数有误');
        }
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $purchase_order_id  = intval($params['purchase_order_id']);
        $purchaseRes = $this->purchaseService->getInfo(['id' => $purchase_order_id]);
        if (0 !== $purchaseRes['code']) {
            return $this->returnJsonData(100400, [], $purchaseRes['message']);
        }
        $condition = [
            'id' => $id,
            'purchase_order_id' => $purchase_order_id
        ];
        unset($params['purchase_order_id']);
        $res = $this->service->update($condition, $params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($id <= 0) {
            return $this->returnJsonData(100001, [], '参数有误');
        }
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $purchase_order_id  = intval($params['purchase_order_id']);
        $purchaseRes = $this->purchaseService->getInfo(['id' => $purchase_order_id]);
        if (0 !== $purchaseRes['code']) {
            return $this->returnJsonData(100400, [], $purchaseRes['message']);
        }
        $condition = [
            'id' => $id,
            'purchase_order_id' => $purchase_order_id
        ];
        unset($params['purchase_order_id']);
        $params['is_del'] = time();
        $res = $this->service->update($condition, $params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
